<?php
/**
 * Copyright (c) 2018,2345
 * 摘    要：User model
 * 作    者：林幸亿
 * 修改日期：2018.05.04
 */

namespace App\Models;

use PDO;


class User extends \Core\Model
{
    private static $db;
    
    public function __construct()
    {
        self::$db = static::getMySqlDB();
    }
    
    /**
     * 功    能：Get all the users as an associative array
     *
     * @return array
     * 修改日期：2018-5-4
     */
    public static function getAll()
    {
        return self::$db->finds('SELECT id, name, email, mobile FROM contacts LIMIT 0,10');
    }
    
    /**
     * 功    能：Get a user
     * 修改日期：2018-5-4
     */
    public static function getOne($id)
    {
        return self::$db->find('SELECT id, name, email, mobile FROM contacts WHERE id=' . $id);
    }
    
    /**
     * 功    能：Get a user
     * 修改日期：2018-5-4
     */
    public static function insert($data=array())
    {
        $sql = "INSERT INTO contacts (name,email,mobile) values('" . $data['name'] . "','" . $data['email'] . "','" . $data['mobile'] . "')";
        $insertId= self::$db->insert($sql); //Return false if unsuccessful, else return insert Id
        return $insertId;
    }
    
    /**
     * 功    能：Get a user
     * 修改日期：2018-5-4
     */
    public static function update($id, $data=array())
    {
        $sql = "UPDATE contacts SET name = '" . $data['name'] . "', email = '" . $data['email'] . "', mobile ='" . $data['mobile'] . "' WHERE id = " . $id;
        $count = self::$db->update($sql); //Return -1 if unsuccessful
        //die(var_dump($count));
        return $count;
    }

    /**
     * 功    能：Get a user
     * 修改日期：2018-5-4
     */
    public static function delete($id)
    {
        $sql = "DELETE FROM contacts WHERE id = " . $id;
        $count = self::$db->delete($sql); //Return -1 if unsuccessful
        //die(var_dump($count));
        return $count;
    }
}
